/**
* @Title: insuredBlacklist_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @author EX-XIEYUWEN001
* @date 2014年10月29日 下午14:46:06
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('InsuredBlackListAuditCtrl', ['$scope','$state', '$http','$stateParams','BaseURL', 'Modal','$modal','IntegrityMedicalService','CommunityService', 
	                                             function($scope,$state, $http,$stateParams, baseUrl, Modal,$modal,IntegrityMedicalService,CommunityService){
		$scope.form={};
		$scope.auditOpinionList=[{code:'01',label:'待审核'},{code:'02',label:'已审核'}];
		
		 var cellTemplate = '<a ng-show="data.processState ==\'01\'" ng-click="operation(data);">审核</a>'+'<a ng-show="data.processState ==\'02\'" ng-click="operation(data);">查看</a>';
		//表头
        $scope.columns = [
							{label:'姓名',map:'name',width:'10%'},
							{label:'性别',map:'gender',width:'5%'},
							{label:'保险号',map:'personCard',width:'7%'},
							{label:'案发时间',map:'strHappenDate',width:'8%'},
							{label:'有效期',map:'expirtDate',width:'15%'},
							
							{label:'电话号码',map:'phoneNumber',width:'7%'},
							{label:'手机号码',map:'mobileNumber',width:'7%'},
							{label:'关注级别',map:'attentionLevel',width:'7%'},
							{label:'关注时限',map:'attentionTimeName',width:'7%'},
							{label:'违规情节',map:'illegal',width:'17%'},
							{label:'状态',map:'processStateName',width:'5%'},
							{label:'操作',map:'opertion',width:'5%',cellTemplate:cellTemplate}
						];
        $scope.agreementListParam = {
                page:"1",
                pageSize:"20"
            };
        //重置函数
        $scope.onReset=function(){
        	$scope.form={};
        	$scope.form.happenDate={
        			start:IntegrityMedicalService.getStartDate(),
					end:IntegrityMedicalService.getEndDate()
        	};
        }
        
        
        $scope.operation = function(data){

            if(data.processState=="01") //待审核
            {   $scope.editor=true;
            	$scope.blackId=data.id;
            	auditPopDialogFun();
            }else if(data.processState=='02'){//已经审核
            	$scope.editor=false;
            	$scope.blackId=data.id;
            	auditPopDialogFun();
            }

        }
        
        
        
        /**
         * 审核弹出框
         */
        function auditPopDialogFun(){
       	 var scope = $scope.$new();
    		var modalInstance = $modal.open({
    		      templateUrl: baseUrl+'views/hcpms/achievements/integrity/insuredBlackCheckDialog_view.html',
    		      backdrop:'static',
    			  size:'sm',
    			  scope:scope,
    			  controller:'InsuredBlackCheckProcessCtrl'
    		      });
    			  
    		}
        
        
        
        
        /**
         * 获取勾选的总记录数
         */
        $scope.checkRows = [];
        
        function findCheck(d) {
         
        	for(var i=0;i< $scope.checkRows .length;i++) {
                 var checkedData =  $scope.checkRows [i];
               if(checkedData.id == d.id) {
                     return i;
                 }
             }
             return -1;
         }

         $scope.$on('fwatable.checked', function(e,d){
             if(findCheck(d) == -1) {
                 $scope.checkRows.push(d);
             }
         });

         $scope.$on('fwatable.unchecked', function(e,d){
             var index = findCheck(d);
             if(index != -1) {
                 $scope.checkRows.splice(index,1);
             }
         });
        
        
        
        
        
         /**
          * 批量提交审核记录
          */
        
          $scope.batchSumit=function(){
        	  var auditStateList=[];
        	  $scope.selectList=[{code:'1',label:'同意'},{code:'0',label:'不同意'}];
        	  $scope.auditObj={};
        	  if($scope.checkRows.length>0){
        		  
        		    var flag=false;
        		    
        		 	for(var i=0;i< $scope.checkRows .length;i++) {
                        var checkedData =  $scope.checkRows [i];
                        var processState=checkedData.processState;
                        if(processState=='01'){ //审核状态
                        	auditStateList.push(checkedData);
                        }
                    }
        		if(auditStateList.length==0){
        		 		 Modal.alert("只能选择审核状态进行批量操作!");
               		      return;
        		}else{
        			   popDialogFun(auditStateList);
        		     }
        			   
        		 
        	  }else{
        		  Modal.alert("请勾选要批量审核的记录!");
        		  return;
        	      }
          }
          
         /**
          * 批量操作弹出框
          */
         function popDialogFun(parameter){
        	 var scope = $scope.$new();
     		var modalInstance = $modal.open({
     		      templateUrl: baseUrl+'views/hcpms/audit/caseCheck/caseCheckBatchSubmitDialog_view.html',
     		      backdrop:'static',
     			  size:'sm',
     			  scope:scope,
     			  controller: function($scope, $http,$modalInstance,Modal){
     				  
     				  
     				     //确定事件
	     				 $scope.batchConfirm=function(){
	     					 
	     					 if($scope.auditObj.opinion){
	     						 
	     						  if($scope.auditObj.opinion=='0'){
		     				    	    if(!$scope.auditObj.opinionDesc){
		     				    	    	Modal.alert('意见描述不能为空!');
		     				    	    	return;
		     				    	    }
		     				       }
	     						 
	     					 }else{
	     						Modal.alert('请选择审核意见!');
 				    	    	return; 
	     					 }
	     					 
	     				     
	     				       
	     				      var batchCaseCode=new Array();
	     				      if(parameter && parameter.length>0){
	     				    	  
	     				    	  
	     				    	 for(var i=0;i< parameter.length;i++) {
		     				    	 var checkedData = parameter[i];
		     				    	 var currentCaseCode=checkedData.id;
		     				    	 batchCaseCode.push(currentCaseCode);
		     				 	    }
		     				      var obj={
		     				    		 caseCodes:batchCaseCode,
		     				    		opinion:$scope.auditObj.opinion,
		     				    		opinionDesc:$scope.auditObj.opinionDesc
		     				    		
		     				      };
		     				     submitSelectAllRecord(obj,$scope.onClose);
	     				    	  
	     				    	  
	     				    	  
	     				      }
	     				      
	     				 }
     				  
     				  
     					//关闭model
     					$scope.onClose = function() {
     						$modalInstance.close();
     					}
     					
     					
     			     }
     		      });
     			  
     		}
         
         
         
         /**
          * 开始提交批量选择的记录
          */
         function submitSelectAllRecord(parameter,closeFun){
        	 $scope.loading = true;
        	 $http({
 				method : 'post',
 				cache : false,
 				url : baseUrl + 'integrityBlackAudit/checkAuditBlackInfo.do',
 				params :{
 					codes:parameter.caseCodes,
 					opinion:parameter.opinion,
 					opinionDesc:parameter.opinionDesc
 				}
 			}).success(function(response, status, headers, config) {
 			    $scope.loading = false;
 				  if(response.flag){
 					   closeFun();
 					    query();
 					  Modal.alert(response.msg);
 				  }else{
 					 closeFun();
 					 Modal.alert(response.msg);
 				  }
 			});
        	 
        	 
        	 
         }
         
        
        //查询按钮事件
		$scope.onSubmit = function(){
			$scope.currentPage = 1;
			$scope.agreementListParam.page = 1;
			$scope.agreementListParam.name=$scope.form.name;
			$scope.agreementListParam.personCard=$scope.form.personCard;
			$scope.agreementListParam.processState=$scope.form.processState;
			if($scope.form.happenDate){
				$scope.agreementListParam.startDate=$scope.form.happenDate.start;
				$scope.agreementListParam.endDate=$scope.form.happenDate.end;
			}else{
				$scope.agreementListParam.startDate='';
				$scope.agreementListParam.endDate='';
			}
			query();
		}
		//弹出查询框
		$scope.queryUserOnClick=function(){
			var scope = $scope.$new();
			var modalInstance = $modal.open({
			      templateUrl: baseUrl+'views/hcpms/achievements/integrity/selectUserInfoDialog_view.html',
			      backdrop:'static',
				  size:'sm',
				  scope:scope,
				  controller: 'selectUserModalCtrl'
			    });
		}
	
		
		/**
		 * 映射性别
		 */
		function isGender(gender){
			if(gender=='男'){
				return "M";
			}else{
				return "F";
			}
		}
		
		/**
		 * 解析有效日期
		 */
		function isExpirtDate(expirtDate){
			var obj={};
			var str=expirtDate.split("至");
			obj.start=str[0];
			obj.end=str[1];
			return obj;
			
		}
		
		
		$scope.commonQuery=function(){
			 query();
		}
		//提交函数
	function query(){
		$scope.loading = true;
          var params = $scope.agreementListParam;
          if(!params.processState){
        	  params.processState="\'01\',\'02\'";
          }
          $http({
				method : 'post',
				cache : false,
				url : baseUrl + 'integrityBlackAudit/queryIntegrityBlackData.do',
				params :params
			}).success(function(response, status, headers, config) {
				 $scope.loading = false;
				if(response){
					$.each(response.result,function(index,item){
						var processState=item.processState;
						var attentionTime=item.attentionTime;
						if(processState=='01'){
							item.processStateName='待审核';
						}else if(processState=='02'){
							item.processStateName='已审核';
						}else if(processState=='03'){
							item.processStateName='已撤销';
						}
						if(attentionTime=='0'){
							item.attentionTimeName='终身';	
						}else if(attentionTime){
							item.attentionTimeName=attentionTime+"年";	
						}
						
						
					});
					$scope.tableData = response.result;
	                $scope.totalCount = response.total;
				}
				
				
			});
		}
		
		var startDate = $stateParams.startDate;
		var endDate = $stateParams.endDate;
		
		if (!startDate 
				&& !endDate) {
				//默认日期 当前日期的前一个月
				CommunityService.initDate("yyyy-MM-dd").success(function(res) {
					endDate = res.formatDate;
					startDate = CommunityService.lastYear(endDate);
					$scope.form.happenDate = {
							start: startDate,
							end: endDate
					};
					query();
				});
			}
			else {
				if (startDate 
					&& !endDate) {
					endDate = CommunityService.backwardYear(startDate);
				} 
				if (!startDate 
					&& endDate) {
					startDate = CommunityService.lastYear(endDate);
				}
				$scope.form.happenDate = {
						start: startDate,
						end: endDate
				};
				query();
			}
		//分页监听
		$scope.$on('pageTool.skipPage',function(e,v){
			 var id=e.targetScope.id;
			 
		     if(id=='parentPageTool'){
		    	 
		    	    $scope.currentPage = v;
					$scope.agreementListParam.page = v;
					query();
		    	 
		     }
			
		});
		
			
	}]);
})();